package Hot100;

import java.util.ArrayList;
import java.util.List;

public class 电话号码的字母组合 {

    String digits = "";
    int len = 0;
    String[] mapping = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    List<String> res = new ArrayList<>();
    char[] comb;

    public List<String> letterCombinations(String digits) {
        if (digits.isEmpty()) return res;
        this.digits = digits;
        len = digits.length();
        comb = new char[len];
        backTracking(0);
        return res;
    }

    private void backTracking(int index) {
        if (index == len) {
            res.add(String.valueOf(comb));
            return;
        }
        String letters = mapping[digits.charAt(index) - '0'];
        for (int i = 0; i < letters.length(); i++) {
            comb[index] = letters.charAt(i);
            backTracking(index + 1);
        }
    }
}
